$(function($){
	
	//清楚空格
	$("input[type='text']").blur(function(){
		$(this).val($.trim($(this).val()));
		var val = $(this).val();
		if(val != "" && val != null){
			var _err = $(this).parent().parent().find("td[class='error']");
			_err.css("display","none");
		}
		
	});
	
	$("select").change("on",function(){
		var val = $(this).val();
		if(val != "" && val != null){
			var _err = $(this).parent().parent().find("td[class='error']");
			_err.css("display","none");
		}
		
	});
	
	
	$("textarea").blur(function(){
		$(this).val($.trim($(this).val()));
	});
	
	$("#doRegist").click(function(){
		var b = validate();
		if(!b){
			return;
		}
		var postUrl = baseContextPath + "userController/regist";
		var vFD = new FormData(document.getElementById('regist_form'));
		var oXHR = new XMLHttpRequest();
		oXHR.addEventListener('load', function(e) {
			var response = e.target.responseText;
			var data = JSON.parse(response);
			if (data.error_code =='0') {
				alert("上传成功");
				window.location.href = baseContextPath;
			} else {
				alert(data.error_msg);
			}
		}, false);
		oXHR.addEventListener('error', function(e) {
			alert("输入参数异常");
			return;
		}, false);
		oXHR.addEventListener('abort', function() {
		}, false);
		oXHR.open('POST', postUrl);
		oXHR.send(vFD);
	});
	
	function validate(){
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var email = $("#email").val();
		if(!email){
			$("td[for='email']").html("不能为空");
			$("td[for='email']").css("display","block");
			return false
		}
		if(!filter.test(email)){
			$("td[for='email']").html("邮箱格式错误");
			$("td[for='email']").css("display","block");
			return false
		}
		
		var password = $("#password").val();
		if(!password){
			$("td[for='password']").html("不能为空");
			$("td[for='password']").css("display","block");
			return false
		}
		
		var subPassword = $("#subPassword").val();
		if(!subPassword){
			$("td[for='subPassword']").html("不能为空");
			$("td[for='subPassword']").css("display","block");
			return false
		}
		
		if(subPassword != password){
			$("td[for='subPassword']").html("确认密码与新密码不一致");
			$("td[for='subPassword']").css("display","block");
			return false
		}
		var userName = $("#userName").val();
		if(!userName){
			$("td[for='userName']").html("不能为空");
			$("td[for='userName']").css("display","block");
			return false
		}
		var isMarry = $("#isMarry option:selected").val();
		if(!isMarry || isMarry==-1){
			$("td[for='isMarry']").html("请选择");
			$("td[for='isMarry']").css("display","block");
			return false
		}
		var city = $("#city option:selected").val();
		var province = $("#province option:selected").val();
		if(!city || !province || province==-1 || city==-1){
			$("td[for='city']").html("请选择省份或城市");
			$("td[for='city']").css("display","block");
			return false
		}
		var birth = $("#birth").val();
		if(!birth){
			$("td[for='birth']").html("不能为空");
			$("td[for='birth']").css("display","block");
			return false
		}
		var height = $("#height").val();
		if(!height){
			$("td[for='height']").html("不能为空");
			$("td[for='height']").css("display","block");
			return false
		}
		var r = /^\d+(\.\d+)?$/;
		if(!r.test(height)){
			$("td[for='height']").html("格式错误，请填写数字");
			$("td[for='height']").css("display","block");
			return false
		}
		var education = $("#education option:selected").val();
		if(!education || education==-1){
			$("td[for='education']").html("请选择");
			$("td[for='education']").css("display","block");
			return false
		}
		var image = $("#image").val();
		if(!image){
			$("td[for='image']").html("不能为空");
			$("td[for='image']").css("display","block");
			return false
		}
		var income = $("#income option:selected").val();
		if(!income || income==-1){
			$("td[for='income']").html("请选择");
			$("td[for='income']").css("display","block");
			return false
		}
		var isHavehouse = $("#isHavehouse option:selected").val();
		if(!isHavehouse || isHavehouse==-1){
			$("td[for='isHavehouse']").html("请选择");
			$("td[for='isHavehouse']").css("display","block");
			return false
		}
		var isHaveCar = $("#isHaveCar option:selected").val();
		if(!isHaveCar || isHaveCar==-1){
			$("td[for='isHaveCar']").html("请选择");
			$("td[for='isHaveCar']").css("display","block");
			return false
		}
		
		return true;
	}
});